﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using Modules.Library.Entities;

namespace Modules.Core {
    public static class Converter {
        public static string KEY = "'";
        public const int MMDDYYYY = 1;
        public const int DDMMYYYY = 2;
        public const int YYYYMMDD = 3;
        public static string ConvertToStringMoney(double money) {
            return money.ToString() + " VND";
        }
        public static string ConvertToDateTime(string value, int type) {
            string[] temp = new string[3];
            string number = "";
            int counter = 0;
            for (int i = 0; i < value.Length; i++) {
                if (Char.IsDigit(value[i])) {
                    number += value[i];
                    if (i == value.Length - 1) {
                        temp[counter] += number;
                    }
                }
                else {
                    temp[counter] = number;
                    number = "";
                    counter++;
                }
            }
            switch (type)
            {
                case DDMMYYYY:
                    return temp[0] + "-" + temp[1] + "-" + temp[2];
                case YYYYMMDD:
                    return temp[2] + "-" + temp[1] + "-" + temp[0];
                default://MMDDYYYY
                    return temp[1] + "-" + temp[0] + "-" + temp[2];
            }
        }
        public static string ConvertToId(string dataId) {
            int number = int.Parse(dataId.Substring(3, dataId.Length - 3)) + 1;
            string code = dataId.Substring(0, 3);
            return code + number.ToString();
        }
        public static string GetStringInsert(object obj, KindOfObject kind) {
            string values;
            switch (kind) {
                case KindOfObject.BY_EMPLOYEE:
                    var emp = (obj as Employee);
                    values = "";
                    values = KEY + emp.MaNhanVien + KEY + ",";
                    values += KEY + emp.HoVaTen + KEY + ",";
                    values += KEY + emp.NgaySinh.ToString("yyyy-MM-dd") + KEY + ",";
                    values += KEY + emp.GioiTinh + KEY + ",";
                    values += KEY + emp.SoCMND + KEY + ",";
                    values += KEY + emp.SoDienThoai + KEY + ",";
                    values += KEY + emp.DiaChi + KEY + ",";
                    values += KEY + emp.Email + KEY + ",";
                    values += KEY + emp.AnhDaiDien + KEY + ",";
                    values += KEY + emp.MaLoaiNhanVien + KEY + ",";
                    values += KEY + emp.UserName + KEY + ",";
                    values += KEY + emp.PassWord + KEY;
                    return values;
                case KindOfObject.BY_CUSTOMER:
                    var cus = (obj as Customer);
                    values = "";
                    values += KEY + cus.MaKhachHang + KEY + ",";
                    values += KEY + cus.HoVaTen + KEY + ",";
                    values += KEY + cus.NgaySinh.ToString("yyyy-MM-dd") + KEY + ",";
                    values += KEY + cus.GioiTinh + KEY + ",";
                    values += KEY + cus.SoCMND + KEY + ",";
                    values += KEY + cus.DiaChi + KEY + ",";
                    values += KEY + cus.SoDienThoai + KEY + ",";
                    values += KEY + cus.Email + KEY + ",";
                    values += KEY + cus.UserName + KEY + ",";
                    values += KEY + cus.PassWord + KEY + ",";
                    values += KEY + cus.NgayDangKy.ToString("yyyy-MM-dd") + KEY + ",";
                    values += KEY + cus.DiemThuong + KEY;
                    return values;
                case KindOfObject.BY_PRODUCT:
                    var pro = (obj as Product);
                    values = "";
                    values += KEY + pro.MaMatHang + KEY + ",";
                    values += KEY + pro.NhaSanXuat + KEY + ",";
                    values += KEY + pro.NhaCungCap + KEY + ",";
                    values += KEY + pro.TenMatHang + KEY + ",";
                    values += KEY + pro.GiaMua + KEY + ",";
                    values += KEY + pro.GiaBan + KEY + ",";
                    values += KEY + pro.SoLuongTonKho + KEY + ",";
                    values += KEY + pro.MaLoaiMatHang + KEY + ",";
                    values += KEY + pro.AnhDaiDien + KEY + ",";
                    values += KEY + pro.GhiChu + KEY;
                    return values;
                case KindOfObject.BY_INVOICE:
                    var invoice = (obj as Invoice);
                    values = "";
                    values += KEY + invoice.MaHoaDonBan + KEY + ",";
                    values += KEY + invoice.MaNhanVienBan + KEY + ",";
                    values += KEY + invoice.MaKhachHangMua + KEY + ",";
                    values += KEY + invoice.NgayBan.ToString("yyyy-MM-dd") + KEY + ",";
                    values += KEY + invoice.TongTien + KEY;
                    return values;

                case KindOfObject.BY_INVOICEDETAILS:
                    var invoiceDetails = (obj as InvoiceDetail);
                    values = "";
                    //values += KEY + invoiceDetails.MaCTHDBan + KEY + ",";
                    values += KEY + invoiceDetails.MaHDBan + KEY + ",";
                    values += KEY + invoiceDetails.MaMH + KEY + ",";
                    values += KEY + invoiceDetails.GiaBan + KEY + ",";
                    values += KEY + invoiceDetails.SoLuongBan + KEY + ",";
                    values += KEY + "MKM0" + KEY + ",";
                    values += KEY + invoiceDetails.TongTien + KEY;
                    return values;
                default:
                    return "";
            }
        }

        public static string GetStringUpdate(object obj, KindOfObject kind) {
            string values;
            switch (kind) {
                case KindOfObject.BY_EMPLOYEE:
                    var emp = (obj as Employee);
                    values = "";
                    values = "MaNV=" + KEY + emp.MaNhanVien + KEY + ",";
                    values += "HoVaTen=" + KEY + emp.HoVaTen + KEY + ",";
                    values += "NgaySinh=" + KEY + emp.NgaySinh.ToString("yyyy-MM-dd") + KEY + ",";
                    values += "GioiTinh=" + KEY + emp.GioiTinh + KEY + ",";
                    values += "CMND=" + KEY + emp.SoCMND + KEY + ",";
                    values += "SoDienThoai=" + KEY + emp.SoDienThoai + KEY + ",";
                    values += "Email=" + KEY + emp.Email + KEY + ",";
                    values += "MaLoaiNV=" + KEY + emp.MaLoaiNhanVien + KEY + ",";
                    values += "UserName=" + KEY + emp.UserName + KEY;
                    return values;
                case KindOfObject.BY_CUSTOMER:
                    var cus = (obj as Customer);
                    values = "";
                    values += "MaKH=" + KEY + cus.MaKhachHang + KEY + ",";
                    values += "HoVaTen=" + KEY + cus.HoVaTen + KEY + ",";
                    values += "NgaySinh=" + KEY + cus.NgaySinh.ToString("yyyy-MM-dd") + KEY + ",";
                    values += "GioiTinh=" + KEY + cus.GioiTinh + KEY + ",";
                    values += "CMND=" + KEY + cus.SoCMND + KEY + ",";
                    values += "DiaChi=" + KEY + cus.DiaChi + KEY + ",";
                    values += "SoDienThoai=" + KEY + cus.SoDienThoai + KEY + ",";
                    values += "Email=" + KEY + cus.Email + KEY + ",";
                    values += "NgayDangKy=" + KEY + cus.NgayDangKy.ToString("yyyy-MM-dd") + KEY + ",";
                    values += "DiemThuong=" + cus.DiemThuong;
                    return values;
                case KindOfObject.BY_PRODUCT:
                    var pro = (obj as Product);
                    values = "";
                    values += "MaMH=" + KEY + pro.MaMatHang + KEY + ",";
                    values += "TenNSX="+ KEY + pro.NhaSanXuat + KEY + ",";
                    values += "TenNCC=" + KEY + pro.NhaCungCap + KEY + ",";
                    values += "TenMH=" + KEY + pro.TenMatHang + KEY + ",";
                    values += "GiaMua=" + KEY + pro.GiaMua + KEY + ",";
                    values += "GiaBan=" + KEY + pro.GiaBan + KEY + ",";
                    values += "SoLuongTonKho=" + KEY + pro.SoLuongTonKho + KEY + ",";
                    values += "MaLoaiMH=" + KEY + pro.MaLoaiMatHang + KEY + ",";
                    values += "AnhDaiDien=" + KEY + pro.AnhDaiDien + KEY + ",";
                    values += "GhiChu=" + KEY + pro.GhiChu + KEY;
                    return values;
                case KindOfObject.BY_INVOICE:
                    var invoice = (obj as Invoice);
                    values = "";
                    values += "MaHDBan=" + KEY + invoice.MaHoaDonBan + KEY + ",";
                    values += "MaNV=" + KEY + invoice.MaNhanVienBan + KEY + ",";
                    values += "MaKH=" + KEY + invoice.MaKhachHangMua + KEY + ",";
                    values += "NgayBan=" + KEY + invoice.NgayBan.ToString("yyyy-MM-dd") + KEY + ",";
                    values += "TongTien=" + KEY + invoice.MaHoaDonBan + KEY;
                    return values;
                case KindOfObject.BY_INVOICEDETAILS:
                    var invoiceDetails = (obj as InvoiceDetail);
                    values = "";
                    values += "MaCTHDBan=" + KEY + invoiceDetails.MaCTHDBan + KEY + ",";
                    values += "MaHDBan=" + KEY + invoiceDetails.MaHDBan + KEY + ",";
                    values += "MaMH=" + KEY + invoiceDetails.MaMH + KEY + ",";
                    values += "GiaBan=" + KEY + invoiceDetails.GiaBan + KEY + ",";
                    values += "SoLuongBan=" + KEY + invoiceDetails.SoLuongBan + KEY + ",";
                    values += "MaKM=" + KEY + "MKM0" + KEY + ",";
                    values += "TongTien=" + KEY + invoiceDetails.TongTien + KEY;
                    return values;
                default:
                    return "";
            }
        }

    //    private static string test(object obj, KindOfObject kind) {
    //        string values;
    //        switch (kind) {
    //            case KindOfObject.BY_EMPLOYEE:
    //                var emp = (obj as Employee);
    //                values = "";
    //                values = emp.MaNhanVien + ",";
    //                values += emp.HoVaTen + ",";
    //                values += emp.NgaySinh.ToString("yyyy-MM-dd") + ",";
    //                values += emp.GioiTinh + ",";
    //                values += emp.SoCMND + ",";
    //                values += emp.SoDienThoai + ",";
    //                values += emp.DiaChi + ",";
    //                values += emp.Email + ",";
    //                values += emp.AnhDaiDien + ",";
    //                values += emp.MaLoaiNhanVien + ",";
    //                values += emp.UserName + ",";
    //                values += emp.PassWord + KEY;
    //                return values;
    //            case KindOfObject.BY_CUSTOMER:
    //                var cus = (obj as Customer);
    //                values = "";
    //                return values;
    //            case KindOfObject.BY_PRODUCT:
    //                var pro = (obj as Product);
    //                values = "";
    //                values += pro.MaMatHang + ",";
    //                values += pro.NhaSanXuat + ",";
    //                values += pro.NhaCungCap + ",";
    //                values += pro.TenMatHang + ",";
    //                values += pro.GiaMua + ",";
    //                values += pro.GiaBan + ",";
    //                values += pro.SoLuongTonKho + ",";
    //                values += pro.MaLoaiMatHang + ",";
    //                values += pro.AnhDaiDien + ",";
    //                values += pro.GhiChu + KEY;
    //                return values;
    //            case KindOfObject.BY_INVOICE:
    //                var invoice = (obj as Invoice);
    //                values = "";
    //                values += invoice.MaHoaDonBan + ",";
    //                values += invoice.MaNhanVienBan + ",";
    //                values += invoice.MaKhachHangMua + ",";
    //                values += invoice.NgayBan.ToString("yyyy-MM-dd") + ",";
    //                values += invoice.MaHoaDonBan + KEY;
    //                return values;
    //        }
    //        return "";
    //    }
    }

    public class BoolToVisibilityConverter : IValueConverter {
        public BoolToVisibilityConverter() { }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            if (value is bool) {
                if ((bool)value) {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }
            return null;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return new NotImplementedException();
        }
    }

    public class BoolToVisibilityScrollBarConverter : IValueConverter {
        public BoolToVisibilityScrollBarConverter() { }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            if (value is bool) {
                if ((bool)value) {
                    return ScrollBarVisibility.Auto;
                }
                return ScrollBarVisibility.Disabled;
            }
            return null;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return new NotImplementedException();
        }
    }
}
